package com.example.zhaoping.config;

import com.example.zhaoping.util.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    //注入token拦截器
    @Autowired
    private Authorization authority;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("/admin/login");
        list.add("/admin/register");
        list.add("/user/login");
        list.add("/user/register");
        list.add("/enterprise/login");
        list.add("/enterprise/register");
        list.add("/register/upload");


        registry.addInterceptor(authority).addPathPatterns("/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/register")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/enterprise/login")
                .excludePathPatterns("/enterprise/register")
                .excludePathPatterns("/register/upload");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")//允许访问全部路径
                //.allowedOrigins("http://127.0.0.1:8080/","http://localhost:8080/","http://192.168.31.39:8080/")  //只允许8080端口访问
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")//请求方式
                .allowCredentials(true)//允许发送token
                .maxAge(3600);//请求时长;
    }
}
